define(function (require, exports, module) {

    require("apps/grid");
    require("apps/easyui");

    exports.init = function (ctx, uuid, config) {
        var _pageComponent = new $.PageDomFactory(uuid);
        var pageListComponent = new $.PageListComponent(uuid, {
            view: {
                grid: "grid",
                myToolbar: "nsdb",
                colbar: "document_tempate"
            },
            configs: {
                gridUrl: ctx + '/app/api/document/myList',
                gridColumns: [
                    {
                        field: 'id',
                        checkbox: true
                    },
                    {
                        field: 'temp1',
                        hidden: true
                    },
                    {
                        field: 'docNow.docName',
                        title: '名称',
                        width: 100,
                        align: 'left'

                    },
                    {
                        field: 'docNow.moduleType.dirValue',
                        title: '类型',
                        width: 50,
                        align: 'left'
                    },
                    {
                        field: 'docNow.docKeyWord',
                        title: '关键字',
                        width: 100,
                        align: 'left'

                    },
                    {
                        field: 'docNow.createUser.userName',
                        title: '创建用户',
                        width: 50,
                        align: 'left'
                    },
                    {
                        field: 'docNow.createTime',
                        title: '创建日期',
                        width: 60,
                        align: 'left'
                    },
                    {
                        field: 'docNow.docBelong.orgName',
                        title: '所属部门',
                        width: 100,
                        align: 'left'
                    },
                    {
                        field: 'docNow.docStatus',
                        title: '状态',
                        width: 50,
                        align: 'left',
                        formatter: function (value, row, index) {
                            return value == "DOC_STATUS_PUBLISHED" ? "已发布" : "未发布";
                        }
                    },
                    {
                        field: 'docNow.version',
                        title: '版本',
                        width: 50,
                        align: 'left',
                        formatter: function (value, row, index) {
                            return "V" + value;
                        }
                    }
                ]
            },
            events: {
                renderColBarBefore: function (type, row) {
                    if (type == "edit") {
                        return WE.currentId == row.docNow.createUser.id;
                    } else if (type == 'range') {
                        return WE.currentId == row.docNow.createUser.id;
                    } else if (type == 'publish') {
                        return WE.currentId == row.docNow.createUser.id && row.docNow.docStatus == "DOC_STATUS_UN_PUBLISHED";
                    }
                    return true;
                },
                onClickColBtn: function (context, btn, row) {
                    switch (btn.key) {
                        case "edit":
                            whenEdit(context, btn.href, row.id);
                            break;
                        case "range":
                            whenSetRange(context, btn.href, row);
                            break;
                        case "publish":
                            whenPublish(context, btn.href, row);
                            break;
                        default :
                            break;
                    }
                },
                onClickToolbarBtn: function (context, btn) {
                    switch (btn.key) {
                        case "search_all":
                            context.searchAll();
                            break;
                        case "search":
                            context.search(btn.href, "文库搜索", "document_search");
                            break;
                        case "add":
                            whenAdd(context, btn.href);
                            break;
                        case "del":
                            whenDel(context, btn.href);
                            break;
                        default :
                            break;
                    }
                }
            }
        });

        var contextWindow = new $.PageWindow();

        function whenAdd(context, href) {
            contextWindow.create({
                href: href,
                method: "document_add_save",
                title: "新增文档",
                callback: function (status) {
                    if (status) {
                        context.grid.datagrid("reload");
                    }
                }
            });
            contextWindow.open();
        }

        function whenDel(context, href) {
            var rows = context.grid.datagrid('getChecked');
            if (rows.length == 0) {
                $.messager.alert('信息', '请先选择一行', 'info');
                context.grid.datagrid('clearSelections');
                return;
            }
            for (var i = 0; i < rows.length; i++) {
                if (rows[i].docNow.docStatus == "DOC_STATUS_PUBLISHED") {
                    $.messager.alert('信息', '已发布的不能删除', 'info');
                    return;
                }
            }
            if (rows.length > 0) {
                $.messager.confirm('确定', '确定要删除吗?', function (result) {
                    if (result) {
                        var ids = [];
                        for (var i = 0; i < rows.length; i++) {
                            ids.push(rows[i].id);
                        }
                        $.post(
                            href,
                            $.utils.buildJson({ids: ids}),
                            function (json) {
                                if ($.isErrorRequest(json)) {
                                    $.messager.alert('信息', json.msg, 'info');
                                } else {
                                    $.messager.alert('信息', '删除成功', 'info');
                                    context.grid.datagrid("reload");
                                }
                            }
                        );
                    }
                });
            }
        };

        function whenEdit(context, href, id) {
            contextWindow.create({
                href: href + "?id=" + id,
                method: "document_edit_save",
                title: "编辑文档",
                callback: function (status) {
                    if (status) {
                        context.grid.datagrid("reload");
                    }
                }
            });
            contextWindow.open();
        }

        function whenSetRange(context, href, row) {
            contextWindow.create({
                href: href + "?id=" + row.id + "&&publish=false",
                method: "document_range_save",
                title: "设置文档'" + row.docNow.docName + "'访问权限",
                callback: function (status) {
                    if (status) {
                        context.grid.datagrid("reload");
                    }
                }
            });
            contextWindow.open();
        }

        function whenPublish(context, href, row) {
            if (row.docRangeType == null) {
                $.messager.defaults = {ok: '设置', cancel: '取消'};
                $.messager.confirm('操作提示', '请先设置访问权限再发布', function (result) {
                    if (result) {
                        contextWindow.create({
                            href: ctx + "/app/api/document/toRange?id=" + row.id + "&publish=true",
                            method: "document_range_save",
                            title: "设置文档'" + row.docNow.docName + "'访问权限",
                            callback: function (status) {
                                if (status) {
                                    context.grid.datagrid("reload");
                                }
                            }
                        });
                        contextWindow.open();
                    }
                });
                $.messager.defaults = {ok: '确定', cancel: '取消'};
            } else {
                $.post(href + "?id=" + row.id, function (json) {
                    if ($.isErrorRequest(json)) {
                        $.messager.alert('信息', json.msg, 'info');
                    } else {
                        $.messager.alert('信息', '发布成功', 'info');
                    }
                    context.grid.datagrid("reload");
                });
            }
        }
    };
});